package com.zw.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.common.entity.product.ProductAttrValueEntity;
import com.zw.gulimall.product.vo.AttrResVo;
import com.zw.gulimall.product.vo.AttrVo;
import com.zw.gulimall.product.vo.ProductAttrValueUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zw.gulimall.product.service.AttrService;
import com.zw.common.utils.PageUtils;
import com.zw.common.utils.R;



/**
 * 商品属性
 *
 * @author zw
 * @email Alisinngai@gmail.com
 * @date 2023-06-11 23:54:02
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
//    /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> attrs = attrService.attrListForSpu(spuId);
        return R.ok().put("data",attrs);
    }
//    /product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateProductAttrValue(@RequestBody List<ProductAttrValueUpdateVo> vos, @PathVariable("spuId") Long spuId){
        attrService.updateProductAttrValue(vos,spuId);
        return R.ok();
    }
///product/attr/base/list/{catelogId}
//    http://localhost:88/api/product/attr/sale/list/0
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String ,Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String attrType
    ){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResVo attr = attrService.getDetailInfo(attrId);

        return R.ok().put("attr", attr);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
